from django.shortcuts import render
from django.http import HttpResponse
from .models import Question,QuestionType,Choice,Answer
from random import sample
from .oo import ProducePaper

# Create your views here.

def index(request):#试卷选择
    
    context = {}
    return render(request,'paper/index.html',context)

def index_yunsuan(request):#生成试卷必要运算

    info = dict(request.POST)
    context_dict = {}
    for question_type,question_info in info.items():
        if question_info[0] != '' and not question_type.startswith('csrf'):
                qu_type = QuestionType.objects.get(pk = question_info[0])
                question = sample(list(qu_type.question_set.all()),int(question_info[1]))
                context_dict[qu_type.question_type] = question

    context = {'context_dict':context_dict}
    print(context_dict)
    return render(request,'paper/index_yunsuan.html',context)
    

def dati(request):#数据渲染
    info = {'单选题':[10,1],
     '填空题':[5,2],
     '判断题':[5,1],
     '英汉互译题':[10,3]}

    context =ProducePaper(info).paper_info_pk
    
    print(context)
    #context = {}
    return render(request,'paper/paper.html',context)

def dati_tijiao_info(request):#接收考试完提交的信息，并运算
    
    context = {}
    return render(request,'paper/dati_tijiao_info.html',context)




























def info_jiekou(request):


	context = {}
	return render(request,'paper/info_jiekou.html',context)
	#return HttpResponse('提交成功')
	
def info_jiekou_yunsuan(request):

	'''<QueryDict: {'csrfmiddlewaretoken': 
	['Xu9GD5AqMDSwOub0pw7Ncjf3DfPB5yjcbcw2Dg
	o9MKAKc196rhH7lZfnWFTQ7R4z'], 
	'question_type': ['1'], 
	'question': ['所有M是P，所有S是M，
	因此不对的结论是（  ）'], 
	'choice': ['所有S是P', '所有S是Px', 
	'所有M是S', ''], 'answer': ['所有M是S']}>
	'''

	info_dict = dict(request.POST)
	type_id = info_dict['question_type'][0]
	if info_dict['question'][0] =='' or info_dict['answer'][0] == '':
	
		return HttpResponse('请检查是否输入问题和答案')
		
	
	question_type = QuestionType.objects.get(pk = type_id)
	
	question = question_type.question_set.create(
				question_text = info_dict['question'][0])
	
	question.answer_set.create(answer_text = info_dict['answer'][0])
	
	for choice in info_dict['choice']:
		if choice != '':
			question.choice_set.create(
				choice_text = choice)
	
	
	

	
	return HttpResponse('添加成功')
	


